<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Controllers
 * @see 		Product categories
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Product_categories extends CI_Controller {
	var $q = '';
	var $updated = '';
	var $failed = '';
	var $name = '';
	var $error = '';
	var $uri_arr = array();
	var $uri_str = '';
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Cat');
		$this->load->model('products_model', 'Pro');
		$this->q = $this->input->get('q', true);
		$this->updated = $this->input->get('updated', true);
		$this->failed = $this->input->get('failed', true);
		$this->name = $this->input->get('name', true);
		$this->error = $this->input->get('error', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'], 
			'q' => $this->q,
			'rowstart' => ''
		);
		$this->uri_str = url_uri($this->uri_arr);
	}

	public function index()
	{
		/*
		* Xu ly du lieu
		*/
		$data = array(
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'url_update' => site_url($GLOBALS['var']['act'].'/update'),
			'uri_str' => $this->uri_str,
			'categories' => array(),
			'info_cat' => $this->Cat->info_cat()
		);
		$rows = $this->Cat->show($this->q);
		if ($rows) $data['categories'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Danh mục sản phẩm',
			'add_link' => current_url().'/update',
			'search' => true,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function update($id = '')
	{
		/*
		* Kiem tra quyen cap nhap
		*/
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		$info = $this->Cat->info($id);
		$data = array(
			'updated' => $this->updated,
			'failed' => $this->failed,
			'error' => $this->error,
			'name' => $this->name,
			'action' => site_url($GLOBALS['var']['act'].'/process').$this->uri_str,
			'info' => $info,
			'categories' => $this->Cat->show($this->q),
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => ($id ? 'Cập nhập' : 'Thêm mới').' danh mục',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/update', $data);
		$this->load->view('footer');
	}

	public function process()
	{
		/*
		* Kiem tra POST method
		*/
		if(!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$name_vn = $this->input->post('name_vn', true);
		if(!token_validation() || $name_vn == '')
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$this->uri_arr['name'] = str_replace('&', '', $name_vn);
		/*
		* Xu ly du lieu
		*/
		$id = $this->input->post('id', true);
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$name_en = $this->input->post('name_en', true);
		$name_en = $name_en ? $name_en : $name_vn;
		$des_vn = $this->input->post('des_vn');
		$des_en = $this->input->post('des_en');
		$des_en = $des_en ? $des_en : $des_vn;
		$data = array(
			'name_vn' => $name_vn,
			'name_en' => $name_en,
			'des_vn' => $des_vn,
			'des_en' => $des_en,
			'parent' => $this->input->post('cat', true),
			'keyword' => url_title(viet_decode($name_vn), '-', true),
            'date_added' => date(TIME_SQL),
            'date_modified' => date(TIME_SQL)
		);
		/*
		* Upload file
		*/
		$this->load->library('upload');
		$upload_data = array();
		if ($_FILES['userfile']['name'])
		{
			$upload_data = upload('', url_title(viet_decode($name_vn), '-', true), CATS);
			if ($upload_data['file_name'])
			{
				make_thumb($upload_data['full_path'], $upload_data['file_path'].'thumbs/'.$upload_data['file_name'], 240, 240);
				$data['icon'] = $upload_data['file_name'];
			}
		}
		/*
		* Ghi du lieu
		*/
		if ($this->Cat->process($data, $id))
		{
			$this->uri_arr['updated'] = 1;
		}
		else
		{
			$this->uri_arr['failed'] = 1;
		}
		/*
		* Chuyen huong
		*/
		if ($id > 0)
		{
			redirect($GLOBALS['var']['act'].'/update/'.$id.url_uri($this->uri_arr));
		}
		else
		{
			redirect($GLOBALS['var']['act'].url_uri($this->uri_arr));
		}
	}
}

/* End of file product_categories.php */
/* Location: ./application/controllers/product_categories.php */